//package server;
//
//import classloader.ModuleManager;
//import classloader.MyClassLoaderByClass;
//import com.myservlet.HttpServlet;
//import org.dom4j.Document;
//import org.dom4j.DocumentException;
//import org.dom4j.Element;
//import org.dom4j.io.SAXReader;
//
//import java.io.ByteArrayInputStream;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.InputStream;
//import java.net.URL;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//public class ServletConfigUtil {
//
//
//
//
//    public static void loadServlet(Map<String, Object> servletMap) throws Exception {
//        File root = new File("Config.webapps");
//        for (File demoRoot : root.listFiles()) { // 遍历各个项目
//            // 先加载jar包
//            File[] jars = new File(demoRoot.getAbsolutePath() + "/WEB-INF/lib").listFiles();
//            List<String> jarList = new ArrayList<>();
//            for (File jar : jars) {
//                jarList.add(jar.getAbsolutePath());
//            }
//            ModuleManager manager = new ModuleManager();
//            manager.setModuleList(jarList);
//            manager.init();
//
//            URL[] urls= new URL[jars.length];
//            for(int i =0;i<jars.length;i++) {
//                urls[i] = jars[i].toURI().toURL();
//            }
//
//            // 加载class
//            MyClassLoaderByClass myClassLoaderByClass = new MyClassLoaderByClass(
//                    demoRoot.getAbsolutePath() + "/WEB-INF/classes/",
//                    urls,
//                    manager
//            );
//
//            // 加载servlet
//            loadServlet(myClassLoaderByClass, demoRoot, servletMap);
//
//        }
//    }
//
//    private static void loadServlet(MyClassLoaderByClass myClassLoaderByClass, File demoRoot, Map<String, Object> servletMap) throws Exception {
//
//        SAXReader saxReader = new SAXReader();
//
//
//        try {
//            Document document = saxReader.read(new File(demoRoot.getAbsolutePath()+"/WEB-INF/web.xml"));
//            Element rootElement = document.getRootElement();
//
//            List<Element> selectNodes = rootElement.selectNodes("//servlet");
//            for (int i = 0; i < selectNodes.size(); i++) {
//                Element element =  selectNodes.get(i);
//                // <servlet-name>lagou</servlet-name>
//                Element servletnameElement = (Element) element.selectSingleNode("servlet-name");
//                String servletName = servletnameElement.getStringValue();
//                // <servlet-class>server.LagouServlet</servlet-class>
//                Element servletclassElement = (Element) element.selectSingleNode("servlet-class");
//                String servletClass = servletclassElement.getStringValue();
//
//
//                // 根据servlet-name的值找到url-pattern
//                Element servletMapping = (Element) rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
//                // /lagou
//                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();
//                Object servlet = myClassLoaderByClass.loadClass(servletClass).newInstance();
//                servletMap.put("/"+demoRoot.getName() + urlPattern, servlet);
////                HttpServlet httpServlet = (HttpServlet) servlet;
////                Map<String, HttpServlet> map = new HashMap<>();
////                map.put("/"+demoRoot.getName() + urlPattern, (HttpServlet)servlet);
////                System.out.println(httpServlet.getClass().getClassLoader());
////
////                Class<?> configClass = myClassLoaderByClass.loadClass("server.Config").newInstance().getClass();
////                System.out.println(configClass.getClassLoader());
////
////                System.out.println();
//            }
//
//
//
//        } catch (DocumentException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//    }
//}
